



/*
-- // checksum failed custom error
exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO


*/


if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Vendor_Distribution_Poles') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Vendor_Distribution_Poles
GO

create view v_tb_Vendor_Distribution_Poles as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Vendor_Distribution_Poles_PoleId] as [Vendor_Distribution_Poles_PoleId]
	,[MapFk] as [MapFk]
	,[SectionFk] as [SectionFk]
	,[SpecieFk] as [SpecieFk]
	,[ClassFK] as [ClassFK]
	,[LengthFk] as [LengthFk]
	,[TreatmentFk] as [TreatmentFk]
	,[Latitude] as [Latitude]
	,[Longitude] as [Longitude]
	,[InspBy] as [InspBy]
	,[Number] as [Number]
	,[AcquisitionDate] as [AcquisitionDate]
	,[Year] as [Year]
	,[LVisit] as [LVisit]
	,[D804] as [D804]
	,[DistributionType] as [DistributionType]
	,[UserFk] as [UserFk]
	,[VendorFk] as [VendorFk]
	,[InspectorFk] as [InspectorFk]
	,[ProjectFk] as [ProjectFk]
	,[Utility_Asset_Distributions_PoleFk] as [Utility_Asset_Distributions_PoleFk]
	,[GuardsQuantity] as [GuardsQuantity]
	,[PoleTag] as [PoleTag]
	,[GroundWireRepair] as [GroundWireRepair]
	,[Billable] as [Billable]
	,[Kv] as [Kv]
	,[Circuit] as [Circuit]
	,[ConductorType] as [ConductorType]
	,[ACSR] as [ACSR]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Vendor_Distribution_Poles_PoleId]), 255) as [FriendlyName]
from
	tb_Vendor_Distribution_Poles
GO







if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Test
GO


create proc up_VendorDistributionPole_Test
	@Vendor_Distribution_Poles_PoleId int
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Vendor_Distribution_Poles_PoleId] 
	from [tb_Vendor_Distribution_Poles] source
	where [source].[Vendor_Distribution_Poles_PoleId] = @Vendor_Distribution_Poles_PoleId)
begin
	return(1)
end
else
begin
	return(0)
end
GO








if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Select
GO

create proc up_VendorDistributionPole_Select
	 @Vendor_Distribution_Poles_PoleId int = null 
	,@MapFk int = null 
	,@SectionFk int = null 
	,@SpecieFk int = null 
	,@ClassFK int = null 
	,@LengthFk int = null 
	,@TreatmentFk int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@InspBy nvarchar(100) = null 
	,@Number nvarchar(100) = null 
	,@AcquisitionDate datetime = null 
	,@Year int = null 
	,@LVisit datetime = null 
	,@D804 nvarchar(50) = null 
	,@DistributionType int = null 
	,@UserFk int = null 
	,@VendorFk int = null 
	,@InspectorFk int = null 
	,@ProjectFk int = null 
	,@Utility_Asset_Distributions_PoleFk int = null 
	,@GuardsQuantity int = null 
	,@PoleTag bit = null 
	,@GroundWireRepair bit = null 
	,@Billable bit = null 
	,@Kv int = null 
	,@Circuit nvarchar(100) = null 
	,@ConductorType int = null 
	,@ACSR int = null 
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	 Vendor_Distribution_Poles_PoleId int
	,MapFk int
	,SectionFk int
	,SpecieFk int
	,ClassFK int
	,LengthFk int
	,TreatmentFk int
	,Latitude float
	,Longitude float
	,InspBy nvarchar(100)
	,Number nvarchar(100)
	,AcquisitionDate datetime
	,Year int
	,LVisit datetime
	,D804 nvarchar(50)
	,DistributionType int
	,UserFk int
	,VendorFk int
	,InspectorFk int
	,ProjectFk int
	,Utility_Asset_Distributions_PoleFk int
	,GuardsQuantity int
	,PoleTag bit
	,GroundWireRepair bit
	,Billable bit
	,Kv int
	,Circuit nvarchar(100)
	,ConductorType int
	,ACSR int
	,[FriendlyName] varchar(255)
	)
	
insert @table
	(
	 [Vendor_Distribution_Poles_PoleId]
	,[MapFk]
	,[SectionFk]
	,[SpecieFk]
	,[ClassFK]
	,[LengthFk]
	,[TreatmentFk]
	,[Latitude]
	,[Longitude]
	,[InspBy]
	,[Number]
	,[AcquisitionDate]
	,[Year]
	,[LVisit]
	,[D804]
	,[DistributionType]
	,[UserFk]
	,[VendorFk]
	,[InspectorFk]
	,[ProjectFk]
	,[Utility_Asset_Distributions_PoleFk]
	,[GuardsQuantity]
	,[PoleTag]
	,[GroundWireRepair]
	,[Billable]
	,[Kv]
	,[Circuit]
	,[ConductorType]
	,[ACSR]
	,[FriendlyName]
	)
select 
	 [Vendor_Distribution_Poles_PoleId]
	,[MapFk]
	,[SectionFk]
	,[SpecieFk]
	,[ClassFK]
	,[LengthFk]
	,[TreatmentFk]
	,[Latitude]
	,[Longitude]
	,[InspBy]
	,[Number]
	,[AcquisitionDate]
	,[Year]
	,[LVisit]
	,[D804]
	,[DistributionType]
	,[UserFk]
	,[VendorFk]
	,[InspectorFk]
	,[ProjectFk]
	,[Utility_Asset_Distributions_PoleFk]
	,[GuardsQuantity]
	,[PoleTag]
	,[GroundWireRepair]
	,[Billable]
	,[Kv]
	,[Circuit]
	,[ConductorType]
	,[ACSR]
	,[FriendlyName]
from v_tb_Vendor_Distribution_Poles
where
		IsNull([v_tb_Vendor_Distribution_Poles].[Vendor_Distribution_Poles_PoleId], -1) = Coalesce(@Vendor_Distribution_Poles_PoleId, [v_tb_Vendor_Distribution_Poles].[Vendor_Distribution_Poles_PoleId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[MapFk], -1) = Coalesce(@MapFk, [v_tb_Vendor_Distribution_Poles].[MapFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[SectionFk], -1) = Coalesce(@SectionFk, [v_tb_Vendor_Distribution_Poles].[SectionFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[SpecieFk], -1) = Coalesce(@SpecieFk, [v_tb_Vendor_Distribution_Poles].[SpecieFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ClassFK], -1) = Coalesce(@ClassFK, [v_tb_Vendor_Distribution_Poles].[ClassFK], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[LengthFk], -1) = Coalesce(@LengthFk, [v_tb_Vendor_Distribution_Poles].[LengthFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[TreatmentFk], -1) = Coalesce(@TreatmentFk, [v_tb_Vendor_Distribution_Poles].[TreatmentFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Vendor_Distribution_Poles].[Latitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Vendor_Distribution_Poles].[Longitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[InspBy], '') = Coalesce(@InspBy, [v_tb_Vendor_Distribution_Poles].[InspBy], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Number], '') = Coalesce(@Number, [v_tb_Vendor_Distribution_Poles].[Number], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[AcquisitionDate], '1/1/1900 12:00AM') = Coalesce(@AcquisitionDate, [v_tb_Vendor_Distribution_Poles].[AcquisitionDate], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Year], -1) = Coalesce(@Year, [v_tb_Vendor_Distribution_Poles].[Year], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[LVisit], '1/1/1900 12:00AM') = Coalesce(@LVisit, [v_tb_Vendor_Distribution_Poles].[LVisit], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[D804], '') = Coalesce(@D804, [v_tb_Vendor_Distribution_Poles].[D804], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[DistributionType], -1) = Coalesce(@DistributionType, [v_tb_Vendor_Distribution_Poles].[DistributionType], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[UserFk], -1) = Coalesce(@UserFk, [v_tb_Vendor_Distribution_Poles].[UserFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[VendorFk], -1) = Coalesce(@VendorFk, [v_tb_Vendor_Distribution_Poles].[VendorFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[InspectorFk], -1) = Coalesce(@InspectorFk, [v_tb_Vendor_Distribution_Poles].[InspectorFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ProjectFk], -1) = Coalesce(@ProjectFk, [v_tb_Vendor_Distribution_Poles].[ProjectFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Utility_Asset_Distributions_PoleFk], -1) = Coalesce(@Utility_Asset_Distributions_PoleFk, [v_tb_Vendor_Distribution_Poles].[Utility_Asset_Distributions_PoleFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[GuardsQuantity], -1) = Coalesce(@GuardsQuantity, [v_tb_Vendor_Distribution_Poles].[GuardsQuantity], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[PoleTag], 0) = Coalesce(@PoleTag, [v_tb_Vendor_Distribution_Poles].[PoleTag], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[GroundWireRepair], 0) = Coalesce(@GroundWireRepair, [v_tb_Vendor_Distribution_Poles].[GroundWireRepair], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Billable], 0) = Coalesce(@Billable, [v_tb_Vendor_Distribution_Poles].[Billable], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Kv], -1) = Coalesce(@Kv, [v_tb_Vendor_Distribution_Poles].[Kv], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Circuit], '') = Coalesce(@Circuit, [v_tb_Vendor_Distribution_Poles].[Circuit], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ConductorType], -1) = Coalesce(@ConductorType, [v_tb_Vendor_Distribution_Poles].[ConductorType], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ACSR], -1) = Coalesce(@ACSR, [v_tb_Vendor_Distribution_Poles].[ACSR], -1)

-- primary select
select * from @table





	-- VendorDistributionImageCollection select

select
	v_tb_Vendor_Distribution_Images.* 
from 
	v_tb_Vendor_Distribution_Images 
	JOIN @table x ON v_tb_Vendor_Distribution_Images.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	




	-- VendorDistributionMeterCollection select

select
	v_tb_Vendor_Distribution_Meters.* 
from 
	v_tb_Vendor_Distribution_Meters 
	JOIN @table x ON v_tb_Vendor_Distribution_Meters.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	




	-- VendorDistributionInspectionResultCollection select

select
	v_tb_Vendor_Distribution_InspectionResults.* 
from 
	v_tb_Vendor_Distribution_InspectionResults 
	JOIN @table x ON v_tb_Vendor_Distribution_InspectionResults.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	




	-- VendorDistributionTransformerCollection select

select
	v_tb_Vendor_Distribution_Transformers.* 
from 
	v_tb_Vendor_Distribution_Transformers 
	JOIN @table x ON v_tb_Vendor_Distribution_Transformers.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	

	
GO 







if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Select_Top1
GO

create proc up_VendorDistributionPole_Select_Top1
	 @Vendor_Distribution_Poles_PoleId int = null 
	,@MapFk int = null 
	,@SectionFk int = null 
	,@SpecieFk int = null 
	,@ClassFK int = null 
	,@LengthFk int = null 
	,@TreatmentFk int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@InspBy nvarchar(100) = null 
	,@Number nvarchar(100) = null 
	,@AcquisitionDate datetime = null 
	,@Year int = null 
	,@LVisit datetime = null 
	,@D804 nvarchar(50) = null 
	,@DistributionType int = null 
	,@UserFk int = null 
	,@VendorFk int = null 
	,@InspectorFk int = null 
	,@ProjectFk int = null 
	,@Utility_Asset_Distributions_PoleFk int = null 
	,@GuardsQuantity int = null 
	,@PoleTag bit = null 
	,@GroundWireRepair bit = null 
	,@Billable bit = null 
	,@Kv int = null 
	,@Circuit nvarchar(100) = null 
	,@ConductorType int = null 
	,@ACSR int = null 
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	  Vendor_Distribution_Poles_PoleId int
	, MapFk int
	, SectionFk int
	, SpecieFk int
	, ClassFK int
	, LengthFk int
	, TreatmentFk int
	, Latitude float
	, Longitude float
	, InspBy nvarchar(100)
	, Number nvarchar(100)
	, AcquisitionDate datetime
	, Year int
	, LVisit datetime
	, D804 nvarchar(50)
	, DistributionType int
	, UserFk int
	, VendorFk int
	, InspectorFk int
	, ProjectFk int
	, Utility_Asset_Distributions_PoleFk int
	, GuardsQuantity int
	, PoleTag bit
	, GroundWireRepair bit
	, Billable bit
	, Kv int
	, Circuit nvarchar(100)
	, ConductorType int
	, ACSR int
	,[FriendlyName] varchar(255)
	)

insert @table
	(
	 [Vendor_Distribution_Poles_PoleId]
	,[MapFk]
	,[SectionFk]
	,[SpecieFk]
	,[ClassFK]
	,[LengthFk]
	,[TreatmentFk]
	,[Latitude]
	,[Longitude]
	,[InspBy]
	,[Number]
	,[AcquisitionDate]
	,[Year]
	,[LVisit]
	,[D804]
	,[DistributionType]
	,[UserFk]
	,[VendorFk]
	,[InspectorFk]
	,[ProjectFk]
	,[Utility_Asset_Distributions_PoleFk]
	,[GuardsQuantity]
	,[PoleTag]
	,[GroundWireRepair]
	,[Billable]
	,[Kv]
	,[Circuit]
	,[ConductorType]
	,[ACSR]
	,[FriendlyName]
	)
select TOP 1
	 [Vendor_Distribution_Poles_PoleId]
	,[MapFk]
	,[SectionFk]
	,[SpecieFk]
	,[ClassFK]
	,[LengthFk]
	,[TreatmentFk]
	,[Latitude]
	,[Longitude]
	,[InspBy]
	,[Number]
	,[AcquisitionDate]
	,[Year]
	,[LVisit]
	,[D804]
	,[DistributionType]
	,[UserFk]
	,[VendorFk]
	,[InspectorFk]
	,[ProjectFk]
	,[Utility_Asset_Distributions_PoleFk]
	,[GuardsQuantity]
	,[PoleTag]
	,[GroundWireRepair]
	,[Billable]
	,[Kv]
	,[Circuit]
	,[ConductorType]
	,[ACSR]
	,[FriendlyName]
from v_tb_Vendor_Distribution_Poles
where
		IsNull([v_tb_Vendor_Distribution_Poles].[Vendor_Distribution_Poles_PoleId], -1) = Coalesce(@Vendor_Distribution_Poles_PoleId, [v_tb_Vendor_Distribution_Poles].[Vendor_Distribution_Poles_PoleId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[MapFk], -1) = Coalesce(@MapFk, [v_tb_Vendor_Distribution_Poles].[MapFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[SectionFk], -1) = Coalesce(@SectionFk, [v_tb_Vendor_Distribution_Poles].[SectionFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[SpecieFk], -1) = Coalesce(@SpecieFk, [v_tb_Vendor_Distribution_Poles].[SpecieFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ClassFK], -1) = Coalesce(@ClassFK, [v_tb_Vendor_Distribution_Poles].[ClassFK], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[LengthFk], -1) = Coalesce(@LengthFk, [v_tb_Vendor_Distribution_Poles].[LengthFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[TreatmentFk], -1) = Coalesce(@TreatmentFk, [v_tb_Vendor_Distribution_Poles].[TreatmentFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Vendor_Distribution_Poles].[Latitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Vendor_Distribution_Poles].[Longitude], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[InspBy], '') = Coalesce(@InspBy, [v_tb_Vendor_Distribution_Poles].[InspBy], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Number], '') = Coalesce(@Number, [v_tb_Vendor_Distribution_Poles].[Number], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[AcquisitionDate], '1/1/1900 12:00AM') = Coalesce(@AcquisitionDate, [v_tb_Vendor_Distribution_Poles].[AcquisitionDate], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Year], -1) = Coalesce(@Year, [v_tb_Vendor_Distribution_Poles].[Year], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[LVisit], '1/1/1900 12:00AM') = Coalesce(@LVisit, [v_tb_Vendor_Distribution_Poles].[LVisit], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[D804], '') = Coalesce(@D804, [v_tb_Vendor_Distribution_Poles].[D804], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[DistributionType], -1) = Coalesce(@DistributionType, [v_tb_Vendor_Distribution_Poles].[DistributionType], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[UserFk], -1) = Coalesce(@UserFk, [v_tb_Vendor_Distribution_Poles].[UserFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[VendorFk], -1) = Coalesce(@VendorFk, [v_tb_Vendor_Distribution_Poles].[VendorFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[InspectorFk], -1) = Coalesce(@InspectorFk, [v_tb_Vendor_Distribution_Poles].[InspectorFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ProjectFk], -1) = Coalesce(@ProjectFk, [v_tb_Vendor_Distribution_Poles].[ProjectFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Utility_Asset_Distributions_PoleFk], -1) = Coalesce(@Utility_Asset_Distributions_PoleFk, [v_tb_Vendor_Distribution_Poles].[Utility_Asset_Distributions_PoleFk], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[GuardsQuantity], -1) = Coalesce(@GuardsQuantity, [v_tb_Vendor_Distribution_Poles].[GuardsQuantity], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[PoleTag], 0) = Coalesce(@PoleTag, [v_tb_Vendor_Distribution_Poles].[PoleTag], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[GroundWireRepair], 0) = Coalesce(@GroundWireRepair, [v_tb_Vendor_Distribution_Poles].[GroundWireRepair], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Billable], 0) = Coalesce(@Billable, [v_tb_Vendor_Distribution_Poles].[Billable], 0)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Kv], -1) = Coalesce(@Kv, [v_tb_Vendor_Distribution_Poles].[Kv], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[Circuit], '') = Coalesce(@Circuit, [v_tb_Vendor_Distribution_Poles].[Circuit], '')
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ConductorType], -1) = Coalesce(@ConductorType, [v_tb_Vendor_Distribution_Poles].[ConductorType], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Poles].[ACSR], -1) = Coalesce(@ACSR, [v_tb_Vendor_Distribution_Poles].[ACSR], -1)

-- primary select
select * from @table


	-- VendorDistributionImageCollection select

select
	v_tb_Vendor_Distribution_Images.* 
from 
	v_tb_Vendor_Distribution_Images 
	JOIN @table x ON v_tb_Vendor_Distribution_Images.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	

	-- VendorDistributionMeterCollection select

select
	v_tb_Vendor_Distribution_Meters.* 
from 
	v_tb_Vendor_Distribution_Meters 
	JOIN @table x ON v_tb_Vendor_Distribution_Meters.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	

	-- VendorDistributionInspectionResultCollection select

select
	v_tb_Vendor_Distribution_InspectionResults.* 
from 
	v_tb_Vendor_Distribution_InspectionResults 
	JOIN @table x ON v_tb_Vendor_Distribution_InspectionResults.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	

	-- VendorDistributionTransformerCollection select

select
	v_tb_Vendor_Distribution_Transformers.* 
from 
	v_tb_Vendor_Distribution_Transformers 
	JOIN @table x ON v_tb_Vendor_Distribution_Transformers.Vendor_Distribution_PoleFk = x.Vendor_Distribution_Poles_PoleId
	
	
	
GO 







if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Insert
GO

create proc up_VendorDistributionPole_Insert
	 @Vendor_Distribution_Poles_PoleId int =null OUTPUT  
	,@MapFk int = null  
	,@SectionFk int = null  
	,@SpecieFk int = null  
	,@ClassFK int = null  
	,@LengthFk int = null  
	,@TreatmentFk int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@InspBy nvarchar(100) = null  
	,@Number nvarchar(100) = null  
	,@AcquisitionDate datetime  
	,@Year int = null  
	,@LVisit datetime = null  
	,@D804 nvarchar(50) = null  
	,@DistributionType int = null  
	,@UserFk int = null  
	,@VendorFk int = null  
	,@InspectorFk int = null  
	,@ProjectFk int = null  
	,@Utility_Asset_Distributions_PoleFk int = null  
	,@GuardsQuantity int = null  
	,@PoleTag bit = null  
	,@GroundWireRepair bit = null  
	,@Billable bit = null  
	,@Kv int = null  
	,@Circuit nvarchar(100) = null  
	,@ConductorType int = null  
	,@ACSR int = null  
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Vendor_Distribution_Poles 
	(
	
	 [MapFk] 
	,[SectionFk] 
	,[SpecieFk] 
	,[ClassFK] 
	,[LengthFk] 
	,[TreatmentFk] 
	,[Latitude] 
	,[Longitude] 
	,[InspBy] 
	,[Number] 
	,[AcquisitionDate] 
	,[Year] 
	,[LVisit] 
	,[D804] 
	,[DistributionType] 
	,[UserFk] 
	,[VendorFk] 
	,[InspectorFk] 
	,[ProjectFk] 
	,[Utility_Asset_Distributions_PoleFk] 
	,[GuardsQuantity] 
	,[PoleTag] 
	,[GroundWireRepair] 
	,[Billable] 
	,[Kv] 
	,[Circuit] 
	,[ConductorType] 
	,[ACSR] 
	)
	values
	(
	
	 @MapFk 
	,@SectionFk 
	,@SpecieFk 
	,@ClassFK 
	,@LengthFk 
	,@TreatmentFk 
	,@Latitude 
	,@Longitude 
	,@InspBy 
	,@Number 
	,@AcquisitionDate 
	,@Year 
	,@LVisit 
	,@D804 
	,@DistributionType 
	,@UserFk 
	,@VendorFk 
	,@InspectorFk 
	,@ProjectFk 
	,@Utility_Asset_Distributions_PoleFk 
	,@GuardsQuantity 
	,@PoleTag 
	,@GroundWireRepair 
	,@Billable 
	,@Kv 
	,@Circuit 
	,@ConductorType 
	,@ACSR 
	)
set @Vendor_Distribution_Poles_PoleId = scope_identity()
select * from v_tb_Vendor_Distribution_Poles where Vendor_Distribution_Poles_PoleId = @Vendor_Distribution_Poles_PoleId

GO






if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Update
GO


create proc up_VendorDistributionPole_Update
	 @Vendor_Distribution_Poles_PoleId int  
	,@MapFk int = null  
	,@SectionFk int = null  
	,@SpecieFk int = null  
	,@ClassFK int = null  
	,@LengthFk int = null  
	,@TreatmentFk int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@InspBy nvarchar(100) = null  
	,@Number nvarchar(100) = null  
	,@AcquisitionDate datetime = null  
	,@Year int = null  
	,@LVisit datetime = null  
	,@D804 nvarchar(50) = null  
	,@DistributionType int = null  
	,@UserFk int = null  
	,@VendorFk int = null  
	,@InspectorFk int = null  
	,@ProjectFk int = null  
	,@Utility_Asset_Distributions_PoleFk int = null  
	,@GuardsQuantity int = null  
	,@PoleTag bit = null  
	,@GroundWireRepair bit = null  
	,@Billable bit = null  
	,@Kv int = null  
	,@Circuit nvarchar(100) = null  
	,@ConductorType int = null  
	,@ACSR int = null  
	,@OriginalVendor_Distribution_Poles_PoleId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalVendor_Distribution_Poles_PoleId is null
	set @OriginalVendor_Distribution_Poles_PoleId = @Vendor_Distribution_Poles_PoleId

-- test
declare @Result bit
execute @Result = up_VendorDistributionPole_Test @OriginalVendor_Distribution_Poles_PoleId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_Distribution_Poles_PoleId)
	raiserror(505060, 16, 1, 'VendorDistributionPole', @x)
	return
end

-- proceed
update tb_Vendor_Distribution_Poles 
set

	 [MapFk] =  @MapFk
	,[SectionFk] =  @SectionFk
	,[SpecieFk] =  @SpecieFk
	,[ClassFK] =  @ClassFK
	,[LengthFk] =  @LengthFk
	,[TreatmentFk] =  @TreatmentFk
	,[Latitude] =  @Latitude
	,[Longitude] =  @Longitude
	,[InspBy] =  @InspBy
	,[Number] =  @Number
	,[AcquisitionDate] = IsNull(@AcquisitionDate, [source].[AcquisitionDate])
	,[Year] =  @Year
	,[LVisit] =  @LVisit
	,[D804] =  @D804
	,[DistributionType] =  @DistributionType
	,[UserFk] =  @UserFk
	,[VendorFk] =  @VendorFk
	,[InspectorFk] =  @InspectorFk
	,[ProjectFk] =  @ProjectFk
	,[Utility_Asset_Distributions_PoleFk] =  @Utility_Asset_Distributions_PoleFk
	,[GuardsQuantity] =  @GuardsQuantity
	,[PoleTag] =  @PoleTag
	,[GroundWireRepair] =  @GroundWireRepair
	,[Billable] =  @Billable
	,[Kv] =  @Kv
	,[Circuit] =  @Circuit
	,[ConductorType] =  @ConductorType
	,[ACSR] =  @ACSR
	
from tb_Vendor_Distribution_Poles source
where
	[source].[Vendor_Distribution_Poles_PoleId] = @OriginalVendor_Distribution_Poles_PoleId


select * from v_tb_Vendor_Distribution_Poles where Vendor_Distribution_Poles_PoleId = @Vendor_Distribution_Poles_PoleId
GO







if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionPole_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionPole_Delete
GO


create proc up_VendorDistributionPole_Delete
	@Vendor_Distribution_Poles_PoleId int
as
	/* 	Initial Generated Date:  miércoles, 04 de agosto de 2010 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_VendorDistributionPole_Test @Vendor_Distribution_Poles_PoleId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_Distribution_Poles_PoleId)
	raiserror(505060, 16, 1, 'VendorDistributionPole', @x)
	return
end

-- proceed
delete from tb_Vendor_Distribution_Poles 
where [Vendor_Distribution_Poles_PoleId] = @Vendor_Distribution_Poles_PoleId

GO


